<?php

class event_enter
{
	var $champ_signup;
	var $event;
	
	var $valid;

	function event_enter($champ_signup, $event)
	{
		$this->champ_signup = $champ_signup;
		$this->event = $event;
	}
	
	function render()
	{
		global $lang, $skin_dir;
		$remStart = $this->event['c_carNumbers'] == 1 ? '' : '<!--';
		$remEnd = $this->event['c_carNumbers'] == 1 ? '' : '-->';
		$carno = $this->champ_signup['cs_no'] == 0 ? '' : $this->champ_signup['cs_no'];
		$number = $this->champ_signup['cs_no'] == '' ? 0 : $this->champ_signup['cs_no'];
		$team = $this->champ_signup['t_name'] == '' ? $lang['Independent'] : $this->champ_signup['t_name'];
		$car = $this->champ_signup['c_manufacturer'] . ' ' . $this->champ_signup['c_model'];
		$driver = $this->champ_signup['cs_drivers'];
		$entryOwner = $this->champ_signup['cs_entryOwner'];
		$division = $this->champ_signup['d_title'];
		$champid = $this->event['c_id'];
		$eventid = $this->event['e_id'];
		include $skin_dir . 'tpl/eventEnter.tpl';
	}
	

}


class event_signup
{
	var $event;
	var $items = Array();

	function event_signup($event)
	{
		$this->event = $event;
		global $db, $lang, $properties;
		include_once('includes/addMessage.php');
		
		//are we logged in?
		if(isset($_SESSION['valid_id']))
		{
			if($this->event['e_openSignUps'] == 0)
			{
				$this->items[] = new addMessage($lang['EventSignupsClosed'], $lang['ChampSingupsClosedTitle'], false, true);
			}
			else if(isset($_POST['enterEvent']))
			{
				//enter event
				if(isset($_POST['event']) && isset($_POST['champid']) && isset($_POST['entryOwner']) &&
					is_numeric($_POST['event']) && is_numeric($_POST['champid']) && is_numeric($_POST['entryOwner']) )
				{
					$q = 'INSERT INTO ' . EVENTSIGNUPS_TABLE . '(es_no, es_driver, es_car, es_team, es_event, es_division, es_signuptime, es_entryOwner) 
						  SELECT  cs_no, cs_driver, cs_car, cs_team, ' . $_POST['event'] . ', cs_div, NOW(), cs_entryOwner
						  FROM ' . CHAMPSIGNUPS_TABLE .'
						  WHERE cs_champ = ' . $_POST['champid'] . ' AND cs_entryOwner = ' . $_POST['entryOwner'];
					$db->sql_query($q);
					if($db->sql_affectedrows() > 0)
					{
						$this->items[] = new addMessage($lang['EventSignedUp'], $lang['ChampSignedUpTitle'], false, true);
					}
					else
					{
						$this->items[] = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle'], false, true);
					}
					
				}
				else
				{
					$this->items[] = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle'], false, true);
				}
			}
			else
			{
				$q = 'SELECT ed_serverPassword, c_serverPassword, ed_date < (DATE_ADD(NOW(), INTERVAL ' . $properties['event_password_viewable'] . ' HOUR)) as password_viewable
					  FROM ' . EVENTSIGNUPS_TABLE . ', ' . EVENTDATES_TABLE . ', ' . CHAMPIONSHIPS_TABLE . ', ' . DIVISIONS_TABLE . '
					  WHERE es_event = ed_event AND es_division = ed_division AND d_id = ed_division AND d_champid = c_id AND 
					  		es_driver = ' . $_SESSION['valid_id'] . ' AND es_event = ' . $this->event['e_id'] ;
				$result = $db->sql_query($q);
				if($row = $db->sql_fetchrow($result)) 
				{
					$this->items[] = new addMessage($lang['EventAlreadySignedUp'], $lang['ChampAlreadySignedUpTitle']);
					if($row['ed_serverPassword'] != '')
					{
						if($row['password_viewable'] == 1)
							$this->items[] = new addMessage(sprintf($lang['ServerPasswordInfo'], $row['ed_serverPassword']),
														$lang['ServerPassword'], false, true);
						else
							$this->items[] = new addMessage(sprintf($lang['ServerPasswordLater'], $properties['event_password_viewable']),
														$lang['ServerPassword'], false, true);
					}
					else if($row['c_serverPassword'] != '')
					{
						if($row['password_viewable'] == 1)
							$this->items[] = new addMessage(sprintf($lang['ServerPasswordInfo'], $row['c_serverPassword']),
														$lang['ServerPassword'], false, true);
						else
							$this->items[] = new addMessage(sprintf($lang['ServerPasswordLater'], $properties['event_password_viewable']),
														$lang['ServerPassword'], false, true);
					}
				}
				else
				{
					//check we havent filled our quota
					$q = "SELECT pf_drivername as user_drivername, d_drivermaxevents, cs_no, t_name, c_manufacturer, c_model, cs_entryOwner,
                                                     d_title, c_multiDriverEntryMin, c_multiDriverEntryMax,
							(SELECT count(*) FROM " . CHAMPSIGNUPS_TABLE . " tc WHERE tc.cs_champ = cs.cs_champ AND tc.cs_entryOwner = cs.cs_entryOwner) as entryDriverCount,
                                                        (SELECT count(*) FROM " . EVENTSIGNUPS_TABLE . " WHERE es_event = {$this->event['e_id']} AND es_division = cs_div) AS entries,
							(SELECT count(*) FROM " . EVENTSIGNUPS_TABLE . " WHERE cs_driver = es_driver AND es_division = cs_div) as eventsTaken,
							(SELECT b_id FROM " . BANS_TABLE . " WHERE b_user = cs_driver AND b_startDate < NOW() AND
							 		 b_endDate > NOW() AND (b_license = {$this->event['c_license']} OR b_license = 0)) as banned,
							(SELECT ed_capacity FROM " . EVENTDATES_TABLE . " WHERE ed_division = cs_div AND ed_event = {$this->event['e_id']} ) AS maxen
						 FROM " . USERS_TABLE . " u, " . DRIVERNAME_TABLE . " d, ".  CARS_TABLE  . " ca, " .  DIVISIONS_TABLE  . ", " .
                                                          CHAMPIONSHIPS_TABLE . " ch, " . CHAMPSIGNUPS_TABLE . " cs
						 LEFT JOIN " . TEAMS_TABLE . " ON t_id = cs_team 
						 WHERE cs_car = ca.c_id AND u.user_id = cs_driver AND u.user_id = d.user_id AND cs_div = d_id AND
                                                       cs_champ = ch.c_id AND cs_driver = {$_SESSION['valid_id']} AND cs_champ = {$this->event['e_championship']}
                                                 GROUP BY cs_entryOwner" ;
                                        
					$result = $db->sql_query($q);
					if($row = $db->sql_fetchrow($result)) 
					{
						if($row['banned'] != '')
						{
                                                    $this->items[] = new addMessage(sprintf($lang['EventSignedUpBanned'],
												 $_SESSION['valid_id']), $lang['ErrorTitle'], false, true);
						}
						else if(!isset($row['maxen']) || $row['maxen'] == NULL)
						{
                                                    $this->items[] = new addMessage($lang['EventSignupNoDivision'], $lang['ErrorTitle'], false, true);
						}
						else if($row['d_drivermaxevents'] > 0 && $row['d_drivermaxevents'] <= $row['eventsTaken'])
						{
                                                    $this->items[] = new addMessage($lang['EventMaxEventsForChamp'], $lang['ErrorTitle'], false, true);
						}
                                                else if($row['c_multiDriverEntryMax'] != 0 && $row['entryDriverCount'] > $row['c_multiDriverEntryMax'] || $row['entryDriverCount'] < $row['c_multiDriverEntryMin'])
                                                {
                                                    $this->items[] = new addMessage(sprintf($lang['EventIncorrectDriverCount'],$row['entryDriverCount'],
                                                                                $row['c_multiDriverEntryMin'],$row['c_multiDriverEntryMax'] ),
                                                                                $lang['ErrorTitle'], false, true);
                                                }
						else
						{
							if($row['entries'] >= $row['maxen'])
							{
								$this->items[] = new addMessage($lang['EventSignupMaxEntries'], $lang['ErrorTitle'], false, true);
							}
							else
							{
								$this->items[] = new addMessage($lang['EventSignupInfo'], $lang['EventSignupTitle'], false, true);
							}
							
							$row['cs_drivers'] = $row['user_drivername'];
							
							$this->items[] = new event_enter($row, $event);
						}
					}
					else
					{
						$this->items[] = new addMessage($lang['ChampNotSignedUp'], $lang['ChampNotSignedUpTitle'], false, true);
					}
				}
			}
		}
	}
	
	function render()
	{
		foreach($this->items as $value)
		{
			$value->render();
		}
	}
	

}

?>